/*
 * @Description: 库管理列表
 * @Author: Zzt
 * @Date: 2022-10-27 15:36:29
 * @LastEditTime: 2022-10-28 18:41:26
 */
import { isNullObj } from '@/utils';
import { Switch, Table } from 'antd';
import Column from 'antd/es/table/Column';
import { useEffect, useState } from 'react';
import { useLIBManageContext } from '../contexts/LIBManageContext';

const PageList = () => {
  const { libraryList, getLibraryList, setSelectKey, selectItems, editLIBManage } =
    useLIBManageContext();
  console.log(libraryList);
  const [loading, setLoading] = useState(false);
  const [status, setStatus] = useState();
  const [listQuery, setListQuery] = useState({
    pageNo: 1,
    pageSize: 10,
    param: { atType: '1' }
  });
  const [total, setTotal] = useState(0);

  /* 切换页码查找列表 */
  useEffect(() => {
    if (listQuery && !isNullObj(listQuery.param)) {
      setLoading(true);
      getLibraryList(listQuery)
        .then((res: any) => {
          setTotal(res?.totalCount);
          setTimeout(() => {}, 2000);
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [listQuery]);
  /**
   * @description 切换页码触发
   * @param {number} page 页码
   * @param {number} pageSize 每页条目数
   */
  const pageChange = (page: number, pageSize: number) => {
    setListQuery((state) => ({
      ...state,
      pageNo: page,
      pageSize
    }));
  };
  /**
   * @description : 表格选项相关设置
   * @param {number} selectedRowKeys 选择的项目的KEY
   */
  const onSelectChange = (selectedRowKeys: number[]) => {
    setSelectKey(selectedRowKeys);
  };
  /**
   * @description:  表格多选配置
   */
  const rowSelection = {
    selectItems,
    onChange: onSelectChange
  };
  const changeStatus = (event: any) => {
    setStatus(event);
  };
  const onChangeStatus = (data: any) => {
    editLIBManage({ ...data, status: !data.status });
  };
  return (
    <>
      <Table
        dataSource={libraryList}
        loading={loading}
        rowKey="id"
        size="small"
        scroll={{ x: 'max-content' }}
        bordered
        rowSelection={rowSelection}
        pagination={{
          current: listQuery.pageNo,
          pageSize: listQuery.pageSize,
          total,
          onChange: pageChange
        }}
      >
        <Column title="序号" dataIndex="id" key="id" align="center" />
        <Column title="库名" dataIndex="description" key="description" align="center" />
        <Column title="英文名" dataIndex="name" key="name" align="center" />
        <Column title="库说明" dataIndex="LIBExplain" key="LIBExplain" align="center" />
        <Column
          title="状态"
          dataIndex="status"
          key="status"
          align="center"
          render={(_text, record: any) => (
            // <Switch
            //   checkedChildren="开启"
            //   unCheckedChildren="关闭"
            //   checked={record.status}
            //   // onChange={changeStatus}
            //   onClick={() => onChangeStatus(record)}
            // />
            <p>开启</p>
          )}
        />
      </Table>
    </>
  );
};
export default PageList;
